home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / v cisle / sadanastroju / bookmark_previews-0.6.5-fx.xpi / components / bookmarkpreviews.js
Text File  |  2008-05-27  |  6KB  |  156 lines

  1. const Ci = Components.interfaces, Cc = Components.classes, Cr = Components.results;
  2. function UnloadComponent() {
  3.  
  4. }
  5. UnloadComponent.prototype = {
  6.   classID: Components.ID("{a3c05366-aa2c-4e1b-9ab0-5168e50cec61}"), // xxx generate guid
  7.   contractID: "@mozdev.org/bookmarkpreviews;1", // XXX generated contractid too
  8.   classDescription: "Bookmark Previews Uninstall Component",
  9.   QueryInterface: function(aIID) {
  10.     if(!aIID.equals(Ci.nsISupports) && !aIID.equals(Ci.nsIObserver) && !aIID.equals(Ci.nsISupportsWeakReference)) // you can claim you implement more interfaces here
  11.       throw Cr.NS_ERROR_NO_INTERFACE;
  12.     return this;
  13.   },
  14.  
  15.   _prefs : null,
  16.   get prefs(){
  17.     if (!this._prefs){
  18.       this._prefs = Components.classes["@mozilla.org/preferences-service;1"].
  19.                     getService(Components.interfaces.nsIPrefService);
  20.       this._prefs = this._prefs.getBranch("extensions.bookmarkpreviews.");
  21.     }
  22.     return this._prefs;
  23.   },
  24.  
  25.   // nsIObserver implementation
  26.   observe: function(aSubject, aTopic, aData) {
  27.     //dump("bookmark previews observer "+aTopic+"\n");
  28.     switch(aTopic) {
  29.       case "xpcom-startup":
  30.         //dump("xpcom-startup");
  31.         // this is run very early, right after XPCOM is initialized, but before
  32.         // user profile information is applied. Register ourselves as an observer
  33.         // for 'quit-application'.
  34.         var obsSvc = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
  35.         obsSvc.addObserver(this, "quit-application", true);
  36.         break;
  37.  
  38.       case "quit-application":
  39.         /*
  40.         Check if this is being uninstalled.
  41.         If so, delete the bookmarkpreviews folder.
  42.         */
  43.         const PREFIX_ITEM_URI    = "urn:mozilla:item:";
  44.         const PREFIX_NS_EM       = "http://www.mozilla.org/2004/em-rdf#";
  45.         const OP_NEEDS_UNINSTALL = "needs-uninstall";
  46.         var itemId = "bookmarkpreviews@mozdev.org";
  47.  
  48.         var rdf = Cc["@mozilla.org/rdf/rdf-service;1"].getService(Ci.nsIRDFService);
  49.         var itemResource = rdf.GetResource(PREFIX_ITEM_URI + itemId);
  50.         if (itemResource) {
  51.           var extmgr = Cc["@mozilla.org/extensions/manager;1"].getService(Ci.nsIExtensionManager);
  52.           var ds = extmgr.datasource;
  53.           var target = ds.GetTarget(itemResource, rdf.GetResource(PREFIX_NS_EM + "opType"), true);
  54.           if (target && target instanceof Ci.nsIRDFLiteral){
  55.             if (target.Value == OP_NEEDS_UNINSTALL){
  56.               this.uninstall();
  57.             }
  58.           }
  59.         }
  60.         break;
  61.       default:
  62.         throw Components.Exception("Unknown topic: " + aTopic);
  63.     }
  64.   },
  65.   uninstall : function(){
  66.     var dir = Components.classes["@mozilla.org/file/directory_service;1"]
  67.           .getService(Components.interfaces.nsIProperties)
  68.           .get("ProfD", Components.interfaces.nsIFile);
  69.     dir.append("bookmarkpreviews");
  70.     if (dir.exists()){
  71.       dir.remove(true);
  72.     }
  73.     /* remove firstrun pref so previews will be created again if reinstalled */
  74.     this.prefs.resetBranch("");
  75.   }
  76. };
  77.  
  78. // constructors for objects we want to XPCOMify
  79. var objects = [UnloadComponent];
  80.  
  81. /*
  82.  * Registration code.
  83.  *
  84.  */
  85. const UNLOAD_OBSERVER_NAME = "Bookmark Previews Uninstall Observer";
  86.  
  87. function FactoryHolder(aObj) {
  88.   this.CID        = aObj.prototype.classID;
  89.   this.contractID = aObj.prototype.contractID;
  90.   this.className  = aObj.prototype.classDescription;
  91.   this.factory = {
  92.     createInstance: function(aOuter, aIID) {
  93.       if(aOuter)
  94.         throw Cr.NS_ERROR_NO_AGGREGATION;
  95.       return (new this.constructor).QueryInterface(aIID);
  96.     }
  97.   };
  98.   this.factory.constructor = aObj;
  99. }
  100.  
  101. var gModule = {
  102.   registerSelf: function (aComponentManager, aFileSpec, aLocation, aType)
  103.   {
  104.     aComponentManager.QueryInterface(Ci.nsIComponentRegistrar);
  105.     for (var key in this._objects) {
  106.       var obj = this._objects[key];
  107.       aComponentManager.registerFactoryLocation(obj.CID, obj.className,
  108.         obj.contractID, aFileSpec, aLocation, aType);
  109.     }
  110.  
  111.     // this can be deleted if you don't need to init on startup
  112.     var catman = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);
  113.     catman.addCategoryEntry("xpcom-startup", UNLOAD_OBSERVER_NAME,
  114.       UnloadComponent.prototype.contractID, true, true);
  115.     //catman.addCategoryEntry("xpcom-shutdown", UNLOAD_OBSERVER_NAME,
  116.     //  UnloadComponent.prototype.contractID, true, true);
  117.   },
  118.  
  119.   unregisterSelf: function(aCompMgr, aFileSpec, aLocation) {
  120.     // this must be deleted if you delete the above code dealing with |catman|
  121.     var catman = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);
  122.     catman.deleteCategoryEntry("xpcom-startup", UNLOAD_OBSERVER_NAME, true);
  123.     // end of deleteable code
  124.  
  125.     aComponentManager.QueryInterface(Ci.nsIComponentRegistrar);
  126.     for (var key in this._objects) {
  127.       var obj = this._objects[key];
  128.       aComponentManager.unregisterFactoryLocation(obj.CID, aFileSpec);
  129.     }
  130.   },
  131.  
  132.   getClassObject: function(aComponentManager, aCID, aIID) {
  133.     if (!aIID.equals(Ci.nsIFactory)) throw Cr.NS_ERROR_NOT_IMPLEMENTED;
  134.  
  135.     for (var key in this._objects) {
  136.       if (aCID.equals(this._objects[key].CID))
  137.         return this._objects[key].factory;
  138.     }
  139.  
  140.     throw Cr.NS_ERROR_NO_INTERFACE;
  141.   },
  142.  
  143.   canUnload: function(aComponentManager) {
  144.     return true;
  145.   },
  146.  
  147.   _objects: {} //FactoryHolder
  148. };
  149.  
  150. function NSGetModule(compMgr, fileSpec)
  151. {
  152.   for(var i in objects)
  153.     gModule._objects[i] = new FactoryHolder(objects[i]);
  154.   return gModule;
  155. }
  156.